package cn.gyx.hrm.service.impl;

import cn.gyx.hrm.constants.BaseConstants;
import cn.gyx.hrm.constants.RedisConstants;
import cn.gyx.hrm.domain.Login;
import cn.gyx.hrm.domain.VipBase;
import cn.gyx.hrm.domain.VipUser;
import cn.gyx.hrm.dto.UserRegisterDto;
import cn.gyx.hrm.exception.GlobalException;
import cn.gyx.hrm.feign.client.LoginFeignClient;
import cn.gyx.hrm.mapper.VipBaseMapper;
import cn.gyx.hrm.mapper.VipUserMapper;
import cn.gyx.hrm.service.IVipUserService;
import cn.gyx.hrm.to.SendVerifyCodeTo;
import cn.gyx.hrm.util.AjaxResult;
import cn.gyx.hrm.util.AssertUtil;
import cn.gyx.hrm.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.netflix.discovery.converters.Auto;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.lang.management.GarbageCollectorMXBean;
import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author gyx
 * @since 2021-12-02
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private BaseMapper baseMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private LoginFeignClient loginFeignClient;

    @Autowired
    private VipBaseMapper vipBaseMapper;

    // 用户提交注册
    @Override
    public void register(UserRegisterDto dto) {
        // 获取提交参数
        String mobile = dto.getMobile();// 获取手机号
        String password = dto.getPassword();// 获取用户密码
        Integer regChannel = dto.getRegChannel();// 获取注册渠道
        String smsCode = dto.getSmsCode(); // 获取短信验证码

        // 判断获取手机号是否位空
        AssertUtil.isNotEmpty(mobile,"手机号为空");
        // 判断手机号是否被注册
        Object one = baseMapper.selectOne(mobile);
        if (one != null) {
            throw new GlobalException("该手机已被注册");
        }
        // 从redis中获取手机验证码并判断是否一致
        String keySmsCode = String.format(RedisConstants.KEY_SMS_CODE, mobile);
        SendVerifyCodeTo sendVerifyCodeTo = (SendVerifyCodeTo)redisTemplate.opsForValue().get(keySmsCode);
        AssertUtil.isNotNull(sendVerifyCodeTo,"无效验证码" );

        // 传入手机号的redis中比较
        AssertUtil.isEquals(sendVerifyCodeTo.getCode(),smsCode ,"输入验证码不一致" );

        // 保存到login
        Login login = new Login();
        login.setUsername(mobile);
        login.setPassword(password);
        login.setType(BaseConstants.Login.STATE_USER);// 设置为用户登录
        AjaxResult ajaxResult = loginFeignClient.save(login);

        AssertUtil.isTrue(ajaxResult.isSuccess(),"登陆失败" );
        AssertUtil.isNotNull(ajaxResult.getResultObj(), "登陆失败");
        // 保存User
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(new Date().getTime());
        vipUser.setPhone(mobile);
        vipUser.setNickName(mobile);
        // 默认成功标识
        long addState = BitStatesUtils.addState(vipUser.getBitState(), BitStatesUtils.OP_ACTIVED);
        // 手机成功标识
        long state = BitStatesUtils.addState(addState, BitStatesUtils.OP_AUTHED_PHONE);
        vipUser.setBitState(state);// 1+8
        super.insert(vipUser);

        // 保存base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(new Date().getTime());
        vipBase.setSsoId(vipUser.getId());
        vipBase.setRegChannel(regChannel);
        vipBaseMapper.insert(vipBase);
        // 删除手机验证码
        redisTemplate.delete(keySmsCode);
    }
}
